package yt.kpool.activity.salesman;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import yt.kpool.R;
import yt.kpool.activity.setting.agent.myguest.HierarchicalModeActivity;
import yt.kpool.adapter.LoginLogAdapter;
import yt.kpool.adapter.SalemanMyAgentAdapter;
import yt.kpool.bean.CurrencyList;
import yt.kpool.bean.LoginLogBean;
import yt.kpool.bean.MyAgentBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.ErrorDialog;
import yt.kpool.view.dialog.LoadingDialog;
import yt.kpool.view.pop.CurrencyPopupWindow;

/**
 * 登录日志
 */
public class LoginLogsActivity extends AppCompatActivity {

    @BindView(R.id.recyleview)
    LRecyclerView mRecyclerView;

    private List<CurrencyList.DataBean> currencyListData;
    private CurrencyPopupWindow currencyPopupWindow;//币种选择
    private LoadingDialog loadingDialog;




    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private List<LoginLogBean.DataBean> dataBeanList;
    private LoginLogAdapter loginLogAdapter;//登录日志列表

    private int pagenum = 1;//页数

    private String userid="";

    public static void start(Activity mactivity, String ID) {
        Intent intent = new Intent(mactivity, LoginLogsActivity.class);
        intent.putExtra("ID", ID);
        mactivity.startActivity(intent);
    }



    public String getID() {
        return getIntent().getStringExtra("ID");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_logs);
        ButterKnife.bind(this);
        userid=getID();
        initTitle();
        initList();
        getData(pagenum,true);
    }

    private void initTitle() {
        TitleControl titleControl=new TitleControl(this);
        titleControl.setTitle("登录日志");
    }

    /**
     * 列表初始化
     */
    private void initList() {

        loadingDialog = new LoadingDialog(this, 0);
        dataBeanList = new ArrayList<>();
        loginLogAdapter = new LoginLogAdapter(this, dataBeanList);

        lRecyclerViewAdapter = new LRecyclerViewAdapter(loginLogAdapter);
        //空白页
        mRecyclerView.setEmptyView(findViewById(R.id.empty));
        findViewById(R.id.tv_refresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                refreshClear();

            }
        });

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(lRecyclerViewAdapter);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader); //设置下拉刷新Progress的样式
        // mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);  //设置下拉刷新箭头
        //设置头部加载颜色
        mRecyclerView.setHeaderViewColor(R.color.system_color, R.color.system_color, R.color.white);
//设置底部加载颜色
        mRecyclerView.setFooterViewColor(R.color.system_color, R.color.system_color, android.R.color.white);

        //分割线
        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.d_1)
                .setPadding(R.dimen.d_0)
                .setColorResource(R.color.colorLine)
                .build();
        mRecyclerView.addItemDecoration(divider);
        mRecyclerView.setOnRefreshListener(new OnRefreshListener() {//下拉刷新
            @Override
            public void onRefresh() {

                refreshClear();
            }
        });
        //停止下拉刷新
        mRecyclerView.setPullRefreshEnabled(true);
        mRecyclerView.setLoadMoreEnabled(true);
        mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {//加载更多
            @Override
            public void onLoadMore() {
                getData(pagenum, false);
            }
        });
    }


    private void getData(int pageIndex, final boolean isLoading) {

        if (NetWorkUtil.check(this)) {
            if (isLoading)
                loadingDialog.show();
            NetHelperNew.LoginLogs(userid,pageIndex+"", new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    Log.i("-->","登录日志："+data);
                    if (isLoading)
                        loadingDialog.dismiss();


                    LoginLogBean myAgentBean = MyGson.getInstance().fromJson(data, LoginLogBean.class);


                    if (myAgentBean.getType() == 1) {

                        mRecyclerView.refreshComplete(20);
                        //更新数据
                        dataBeanList.addAll(myAgentBean.getData());

                        loginLogAdapter.notifyDataSetChanged();
                        pagenum++;

                    }else
                    {
                        new ErrorDialog(LoginLogsActivity.this, myAgentBean.getContent()).show();
                    }



                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    if (isLoading)
                        loadingDialog.dismiss();
                    mRecyclerView.refreshComplete(20);
                    ToastUtil.showToast(getApplicationContext(), msg);
                }
            });
        }

    }

    /**
     * 刷新数据
     */
    private void refreshClear() {
        dataBeanList.clear();
        loginLogAdapter.notifyDataSetChanged();
        pagenum=1;
        getData(pagenum,true);
    }
}
